<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

$setting_avatar['length']    = 40;
$setting_avatar['size']      = 102400;
$setting_avatar['type']     = ".gif,.jpg,.png";

// cp_avatar_show_value
function cp_avatar_show_value($id)
{
    global $SETTING,$LANGUAGE,$MEMBER,$select_option,$db;
    $ajax_response = new xajaxResponse();
    $mid = $MEMBER['id'];
    $member_info = func::db_select(DB_TABLE_MEMBER_DETAIL, "*", "mid=".$mid);
    $member_info = $member_info['0'];
    $file_str   = "";
    if (!empty($member_info)){
        if($member_info['avatar_type'] == "1" ){
            $avatar_path_dir_filename = $SETTING['dir_avatar_select']."/".$member_info['avatar_value'];
        }elseif($member_info['avatar_type'] == "2" ){
            $avatar_path_dir_filename = $member_info['avatar_value'];
        }elseif($member_info['avatar_type'] == "3" ){
            $avatar_path_dir_filename = $SETTING['dir_member_attach']."/".$member_info['avatar_value'];
        }else{
            $avatar_path_dir_filename = "";
        }
        if (!empty($avatar_path_dir_filename)) {
            $file_str   = "<img src=\"".$avatar_path_dir_filename."\" width=\"96\" border=\"0\" align=\"absmiddle\" style=\"border:1px solid #CCCCCC;background-color:#EEEEEE;\" />";
        }
    }
    if (empty($file_str)) {
        $file_str   = "&nbsp;";
    }
    $ajax_response->assign($id,"innerHTML",$file_str);
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "cp_avatar_show_value");

// cp_avatar_select_type
function cp_avatar_select_type($id, $type)
{
    global $SETTING,$LANGUAGE,$MEMBER,$select_option,$db;
    $ajax_response = new xajaxResponse();
    if ($type == "1") {
        $avatar_list = func::return_dir($SETTING['dir_avatar_select'], "Thumbs.db,.svn");
        $avatar_str = '';
        foreach($avatar_list as $avatar_pic){
            if($member_info['member_type'] == $type && $member_info['member_value'] == $avatar_pic){ $str_value = "checked"; }else{ $str_value = ""; }
            $avatar_str .= '<div class="member_avatar_list"><img src="'.$SETTING['dir_avatar_select'].'/'.$avatar_pic.'" width="96" height="96" border="0" /><br /><input type="radio" id="avatar_value" name="avatar_value" value="'.$avatar_pic.'" '.$str_value.'></div> ';
        }
        $ajax_response->assign($id, "innerHTML", $avatar_str);
    } elseif ($type == "2") {
        if($type == $member_info['member_type']){ $str_value = $member_info['member_value']; }else{ $str_value = ""; }
        $ajax_response->assign($id, "innerHTML", $LANGUAGE['s']['cp_avatar']['avatar_type_url'].'<input type="text" id="avatar_value" name="avatar_value" size="64" value="'.$str_value.'" class="input" />');
    } elseif ($type == "3") {
        $ajax_response->assign($id, "innerHTML", $LANGUAGE['s']['cp_avatar']['avatar_type_upload'].'<span id="avatar_value_input" name="avatar_value_input"><input type="file" id="avatar_value" name="avatar_value" size="30" style="width:250px;" class="input" /></span>');
    } else {
        $ajax_response->assign($id, "innerHTML", '&nbsp;');
    }
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "cp_avatar_select_type");

// cp_avatar_upload_return
function cp_avatar_upload_return($state,$type,$form,$submit,$prompt,$msg)
{
    global $LANGUAGE,$LANGLIST,$SETTING,$MEMBER,$setting_cover;
    $ajax_response = new xajaxResponse();
    $ajax_response->assign($form,"action","javascript:void(null);");
    $ajax_response->assign($form,"target","_self");

    $ajax_response->script("xajax_cp_avatar_show_value('avatar_value_show');");
    $ajax_response->script("xajax_cp_avatar_select_type('avatar_type_show','".$type."');");

    if ($state != "1") {
        $ajax_response->assign($prompt, "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_avatar']['upload_failed']." [".$msg."]</span>");
    } else {
        $ajax_response->assign($prompt, "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_avatar']['submit_amend_succeed']."</span>");
    }
    $ajax_response->assign($submit,"value",$LANGUAGE['s']['cp_avatar']['submit_amend']);
    $ajax_response->assign($submit,"disabled",false);
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "cp_avatar_upload_return");

function cp_avatar($value)
{
    global $SETTING,$LANGUAGE,$CONFIG,$CONFIGURE,$SESSION,$PROMPT,$MEMBER,$db,$sys,$c;
    $ajax_response = new xajaxResponse();
    $error = false;
    //$ajax_response->alert(print_r($value, true)); $ajax_response->assign("cp_avatar_submit","disabled",false); return $ajax_response;

    // check the avatar
    $prompt     = "avatar_prompt";
    $submit     = "cp_avatar_submit";
    if($value['avatar_type'] == "1"){
        if(!preg_match("/[0-9a-z-_]+/i",$value['avatar_value'])){
            $error = true;
            $ajax_response->assign($prompt, "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_avatar']['avatar_type_1_error']."</span>");
        }else{
            $avatar_type    = $value['avatar_type'];
            $avatar_value   = $value['avatar_value'];
        }
    }elseif($value['avatar_type'] == "2"){
        if(!preg_match("/[0-9a-z-_\:\/\.\?\=&]+/i",$value['avatar_value'])){
            $error = true;
            $ajax_response->assign($prompt, "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_avatar']['avatar_type_2_error']."</span>");
        }else{
            $avatar_type    = $value['avatar_type'];
            $avatar_value   = $value['avatar_value'];
        }
    }elseif($value['avatar_type'] == "3"){
        $form       = "cp_avatar";
        $action     = $CONFIGURE['common']['control_index']."?act=cp_avatar&ope=save_upload";
        $target     = $form."_upload_target";

        $ajax_response->assign($prompt, "innerHTML", "<img src=\"".$SETTING['dir_images']."/loading.gif\" border=\"0\" align=\"absmiddle\" />");
        $ajax_response->remove($target);
        $ajax_response->append($prompt, "innerHTML", "<iframe id=\"".$target."\" name=\"".$target."\" width=\"0\" height=\"0\" scrolling=\"no\" frameborder=\"no\" src=\"about:blank\" style=\"display:none;\"></iframe>");
        $ajax_response->assign($form,"action",$action);
        $ajax_response->assign($form,"target",$target);
        $ajax_response->script("document.".$form.".submit();");
    }else{
        $avatar_type    = 0;
        $avatar_value   = "";
    }

    if($value['avatar_type'] != "3"){
        if($error){
            $ajax_response->assign($submit,"value",$LANGUAGE['s']['cp_avatar']['submit_amend']);
            $ajax_response->assign($submit,"disabled",false);
        }else{
            $sql = "UPDATE ".DB_TABLE_MEMBER_DETAIL." SET
                        avatar_type     = '".addslashes($avatar_type)."',
                        avatar_value    = '".addslashes($avatar_value)."'
                    WHERE mid= '".$MEMBER['id']."'";
            if (!$db->Execute($sql)) {
                echo $db->ErrorMsg();
            }else{
                $ajax_response->script("xajax_cp_avatar_show_value('avatar_value_show');");
                $ajax_response->script("xajax_cp_avatar_select_type('avatar_type_show','".$avatar_type."', '".$avatar_type."','".$avatar_value."');");
                $ajax_response->assign($prompt, "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_avatar']['submit_amend_succeed']."</span>");
                $ajax_response->assign($submit,"value",$LANGUAGE['s']['cp_avatar']['submit_amend']);
                $ajax_response->assign($submit,"disabled",false);
            }
        }
    }

    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "cp_avatar");

if ($sys->get['ope'] == "save_upload") {

    $bwupload->lang_msg = array(
        '0'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['0'],
        '1'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['1'],
        '2'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['2'],
        '3'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['3'],
        '4'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['4'],
        '6'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['6'],
        '7'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['7'],
        '8'             => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['8'],
        'invalid'       => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['invalid'],
        'error_size'    => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['error_size'],
        'error_type'    => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['error_type'],
        'upload_failed' => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['upload_failed'],
        'error_empty'   => $LANGUAGE['s']['_public']['lib_class_bwupload_class']['error_empty'],
    );
    $bwupload->set_size($setting_avatar['size']);
    $bwupload->set_type($setting_avatar['type']);

    $mid            = $MEMBER['id'];
    $type           = "3";

    $result_state   = "1";
    $error_msg      = "";

    if (!preg_match("/^[0-9]+$/",$mid)) {
        $result_state = "0";
        $error_msg = $LANGUAGE['s']['cp_avatar']['avatar_mid_error'];
    } elseif (!preg_match("/^[3]{1}$/",$type)) {
        $result_state = "0";
        $error_msg = $LANGUAGE['s']['cp_avatar']['avatar_type_error'];
    }

    if ($result_state == "1") {
        $member_info    = func::db_select(DB_TABLE_MEMBER_DETAIL, "*", "mid=".$mid);
        $member_info    = $member_info[0];
        $dir_multiple   = floor($mid/10000);
        $dir            = ($dir_multiple*10000+1)."_".(($dir_multiple+1)*10000)."/";
        $path_dir       = $SETTING['dir_member_attach']."/".$dir;

        if (!file_exists($path_dir)) { func::make_dir($path_dir); }
        if ( !$bwupload->upload_file($path_dir,"avatar_value") ) {
            $result_state   = "0";
            $error_msg      = $bwupload->error_msg."<!-- upload error -->";
        } else {
            $result_state   = $bwupload->proceed['avatar_value']['succeed'];
            $error_msg      = $bwupload->proceed['avatar_value']['error']."<!-- upload error message -->";
            if ($result_state == "1") {
                if ($member_info['avatar_type'] == "3" && !empty($member_info['avatar_value'])){
                    if (file_exists($SETTING['dir_member_attach']."/".$member_info['avatar_value'])) {
                        @chmod($SETTING['dir_member_attach']."/".$member_info['avatar_value'],$SETTING['chmod_mode_file']);
                        @unlink($SETTING['dir_member_attach']."/".$member_info['avatar_value']);
                    }
                }
                $sql_data = array(
                    "avatar_type"   => "'".$type."'",
                    "avatar_value"  => "'".$dir.$bwupload->proceed['avatar_value']['result_name']."'",
                );
                $result = func::db_update(DB_TABLE_MEMBER_DETAIL, $sql_data, "mid=".$mid);
                if (!$result) {
                    if (file_exists($path_dir.$bwupload->proceed['avatar_value']['result_name'])) {
                        @chmod($path_dir.$bwupload->proceed['avatar_value']['result_name'], $SETTING['chmod_mode_file']);
                        @unlink($path_dir.$bwupload->proceed['avatar_value']['result_name']);
                    }
                    $result_state = "0";
                    $error_msg = $LANGUAGE['s']['cp_avatar']['avatar_uoload_db_error'];
                }
            }
        }
    }
    $CONFIGURE['common']['if_output_template'] = "0";
    $bwjs->write("parent.xajax_cp_avatar_upload_return('".$result_state."',
               '3',
               'cp_avatar',
               'cp_avatar_submit',
               'avatar_prompt',
               '".$error_msg."');");
}
$public_var['page_place'][] = $LANGUAGE['s']['cp_avatar']['page_place_upper'];
$public_var['page_place'][] = $LANGUAGE['s']['cp_avatar']['page_place'];
?>
